using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Aga.Psad.Backend.Repositories;
using AutoMapper;
using Aga.Psad.Backend.Entities;
using Aga.Psad.Mvc.Models.Clientes;
using Aga.Psad.Mvc.Models.Auditoria;

namespace Aga.Psad.Mvc.Controllers
{
    [Authorize(Roles="Administrador")]
    public class AuditoriaController : ControllerBase
    {
        public static string CLIENTES = "CLIENTES";
        public static string MES = "MES";

        public AuditoriaController()
        {
            Mapper.CreateMap<Cliente, ClienteViewModel>();
            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteViewModel>();
            Mapper.CreateMap<Auditoria, AuditoriaViewModel>();

            ViewData["Menu"] = "audit";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ReporteDeAccesosPorCliente()
        {
            CargarClientesEnViewData();

            CargarMesesEnViewData();

            return View();
        }

        private void CargarClientesEnViewData()
        {
            ClienteRepository repository = new ClienteRepository();
            var clientes = repository.GetAll();

            List<SelectListItem> listItemsClientes = clientes.Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text = c.RazonSocial
            }).ToList();

            ViewData[CLIENTES] = listItemsClientes;
        }

        private void CargarMesesEnViewData()
        {
            List<SelectListItem> listItemsMes = new List<SelectListItem>
            {
                new SelectListItem { Value = "1", Text = "Enero", Selected = DateTime.Now.Month == 1 },
                new SelectListItem { Value = "2", Text = "Febrero", Selected = DateTime.Now.Month == 2 },
                new SelectListItem { Value = "3", Text = "Marzo", Selected = DateTime.Now.Month == 3 },
                new SelectListItem { Value = "4", Text = "Abril", Selected = DateTime.Now.Month == 4 },
                new SelectListItem { Value = "5", Text = "Mayo", Selected = DateTime.Now.Month == 5 },
                new SelectListItem { Value = "6", Text = "Junio", Selected = DateTime.Now.Month == 6 },
                new SelectListItem { Value = "7", Text = "Julio", Selected = DateTime.Now.Month == 7 },
                new SelectListItem { Value = "8", Text = "Agosto", Selected = DateTime.Now.Month == 8 },
                new SelectListItem { Value = "9", Text = "Septiembre", Selected = DateTime.Now.Month == 9 },
                new SelectListItem { Value = "10", Text = "Octubre", Selected = DateTime.Now.Month == 10 },
                new SelectListItem { Value = "11", Text = "Noviembre", Selected = DateTime.Now.Month == 11 },
                new SelectListItem { Value = "12", Text = "Diciembre", Selected = DateTime.Now.Month == 12 }
            };

            ViewData[MES] = listItemsMes;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ReporteDeAccesosPorCliente(string cliente, string mes)
        {
            ClienteRepository clientes = new ClienteRepository();
            AuditoriaRepository repository = new AuditoriaRepository();

            int clienteId = Convert.ToInt32(cliente);
            int mesNum = Convert.ToInt32(mes);

            Dictionary<ClienteDeClienteViewModel, List<AuditoriaViewModel>> auditClientes = new Dictionary<ClienteDeClienteViewModel, List<AuditoriaViewModel>>();

            var cli = clientes.GetById(clienteId);
            cli.Clientes.Load();

            var registros = repository.GetByMes(mesNum);

            foreach (var c in cli.Clientes.Where(c => !String.IsNullOrEmpty(c.Usuario)))
            {
                List<Auditoria> audit = new List<Auditoria>();

                foreach (var usr in c.Usuario.Split('|').Distinct())
                {
                    audit.AddRange(registros.Where(r => r.Usuario.Equals(usr)).OrderBy(r => r.Fecha));
                }

                ClienteDeClienteViewModel cliModel = Mapper.Map<ClienteDeCliente, ClienteDeClienteViewModel>(c);
                List<AuditoriaViewModel> auditModel = Mapper.Map<List<Auditoria>, List<AuditoriaViewModel>>(audit);

                auditClientes.Add(cliModel, auditModel);
            }

            List<Auditoria> auditCliente = new List<Auditoria>();

            foreach (var usr in cli.Usuario.Split('|').Distinct())
            {
                auditCliente.AddRange(registros.Where(r => r.Usuario.Equals(usr)).OrderBy(r => r.Fecha));
            }


            ReporteDeAccesosPorClienteViewModel model = new ReporteDeAccesosPorClienteViewModel
            {
                Cliente = Mapper.Map<Cliente, ClienteViewModel>(cli),
                Accesos = Mapper.Map<List<Auditoria>, List<AuditoriaViewModel>>(auditCliente),
                Mes = mesNum,
                Registros = auditClientes
            };

            return View("ReporteDeAccesosPorClienteVistaImpresion", model);
        }

        public ActionResult ReporteDeAccesos()
        {
            CargarMesesEnViewData();

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ReporteDeAccesos(string mes)
        {
            ClienteRepository clientesRepository = new ClienteRepository();
            AuditoriaRepository repository = new AuditoriaRepository();

            int mesNum = Convert.ToInt32(mes);

            var registros = repository.GetByMes(mesNum);

            var clientes = clientesRepository.GetAll();

            Dictionary<ClienteViewModel, List<AuditoriaViewModel>> auditClientes = new Dictionary<ClienteViewModel, List<AuditoriaViewModel>>();

            foreach (var c in clientes.Where(c => !String.IsNullOrEmpty(c.Usuario)))
            {
                List<Auditoria> audit = new List<Auditoria>();

                foreach (var usr in c.Usuario.Split('|').Distinct())
                {
                    audit.AddRange(registros.Where(r => r.Usuario.Equals(usr)).OrderBy(r => r.Fecha));
                }

                ClienteViewModel cliModel = Mapper.Map<Cliente, ClienteViewModel>(c);
                List<AuditoriaViewModel> auditModel = Mapper.Map<List<Auditoria>, List<AuditoriaViewModel>>(audit);

                auditClientes.Add(cliModel, auditModel);
            }

            var model = new ReporteDeAccesosViewModel
            {
                Mes = mesNum,
                Resultados = auditClientes
            };

            return View("ReporteDeAccesosVistaImpresion", model);
        }

    }
}
